package com.guan.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guan.eduservice.client.VodClient;
import com.guan.eduservice.entity.EduVideo;
import com.guan.eduservice.mapper.EduVideoMapper;
import com.guan.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程视频（课程小节） 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-01-01
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    //注入vodClient
    @Autowired
    private VodClient vodClient;


    /**
     * 删除小节
     * @param courseId
     */
    @Override
    public void removeVideoByCoureId(String courseId) {
        //根据课程id查所有视频id
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        //设置查询指定列
        wrapperVideo.select("video_source_id");
        List<EduVideo> eduVideoList = baseMapper.selectList(wrapperVideo);
        //map映射 只获取视频id
        List<String> videoIdList =
                eduVideoList.stream()
                            .map(video -> video.getVideoSourceId())
                            .collect(Collectors.toList());
        if (!videoIdList.isEmpty()){
            //批量删除视频
            vodClient.deleteBatch(videoIdList);
        }

        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        //删小节
        baseMapper.delete(wrapper);
    }
}
